package kr.co.jnu.UND;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore.Images;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class SetBgActivity extends Activity {
	static final int REQ_PICK = 01;
	static final String fileName = "bg.jpg";

	LinearLayout l;
	Button btn1, btn2;
	TextView tv1;
	ImageView img;
	Bitmap bg;
	File bgPath;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		

		l = new LinearLayout(this);
		l.setOrientation(LinearLayout.VERTICAL);

		LinearLayout lh = new LinearLayout(this);
		lh.setOrientation(LinearLayout.HORIZONTAL);
		lh.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));

		btn1 = new Button(this);
		btn1.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		btn1.setId(1);
		btn1.setText("Choose Image");
		btn1.setOnClickListener(onClick);

		btn2 = new Button(this);
		btn2.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		btn2.setId(2);
		btn2.setText("Set to Background");
		btn2.setOnClickListener(onClick);

		tv1 = new TextView(this);
		tv1.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));

		tv1.setText("touch image to rotate.");

		img = new ImageView(this);
		img.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT));
		img.setImageResource(R.drawable.ic_launcher);
		img.setOnClickListener(onImgClick);

		lh.addView(btn1);
		lh.addView(btn2);

		l.addView(lh);
		l.addView(tv1);
		l.addView(img);
		setContentView(l);
		
		String root = Environment.getExternalStorageDirectory().toString();
		bgPath = new File(root + "/und");
		bgPath.mkdirs();
		File file = new File(bgPath, fileName);
		
		if (file.exists()) {			
			bg = BitmapFactory.decodeFile(file.getPath());
			img.setImageBitmap(bg);
		}
	}

	View.OnClickListener onClick = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			int id = v.getId();
			switch (id) {
			case 1:
				Intent i = new Intent(Intent.ACTION_PICK);
				i.setType(android.provider.MediaStore.Images.Media.CONTENT_TYPE);
				i.setData(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
				startActivityForResult(i, REQ_PICK);
				break;
			case 2:
				SaveBitmap(bg);				
				break;
			}
		}
	};

	View.OnClickListener onImgClick = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			Toast.makeText(getApplicationContext(), "rotate 90", 0).show();
			bg = rotate(bg, 90);
			img.setImageBitmap(bg);
		}
	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == REQ_PICK) {
			if (resultCode == Activity.RESULT_OK) {
				Uri uri = data.getData();

				try {
					bg = Images.Media.getBitmap(getContentResolver(), uri);
					int x = bg.getWidth();
					int y = bg.getHeight();
					int t =x;
					if(x>y)
						t=y;					
					while(t > 1000){
						bg = Bitmap.createScaledBitmap(bg, (int)(x/2), (int)(y/2), false);
						x = bg.getWidth();
						y = bg.getHeight();
						t =x;
						if(x>y)
							t=y;
					}
					img.setImageBitmap(bg);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public Bitmap rotate(Bitmap bitmap, int degrees) {
		if (degrees != 0 && bitmap != null) {
			Matrix m = new Matrix();
			m.setRotate(degrees, (float) bitmap.getWidth() / 2,
					(float) bitmap.getHeight() / 2);
			try {
				Bitmap converted = Bitmap.createBitmap(bitmap, 0, 0,
						bitmap.getWidth(), bitmap.getHeight(), m, true);
				if (bitmap != converted) {
					bitmap.recycle();
					bitmap = converted;
				}
			} catch (OutOfMemoryError ex) {
				Toast.makeText(getApplicationContext(), "not enough memory", 0).show();
			}
		}
		return bitmap;
	}

	private void SaveBitmap(Bitmap bmp) {

		File file = new File(bgPath, fileName);
		if (file.exists())
			file.delete();
		try {
			FileOutputStream out = new FileOutputStream(file);
			bmp.compress(Bitmap.CompressFormat.JPEG, 90, out);
			out.flush();
			out.close();
//			tv1.setText(bgPath + "/" + fileName);
		} catch (Exception e) {
			Toast.makeText(getApplicationContext(), "err", 0).show();
			e.printStackTrace();
		}
		finish();
	}
}
